package cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import cn.dnui_xqh414.dnuifood_16110100414.FoodDetailActivity;
import cn.dnui_xqh414.dnuifood_16110100414.R;
import cn.dnui_xqh414.dnuifood_16110100414.ShopFoodListActivity;
import cn.dnui_xqh414.dnuifood_16110100414.bean.AllshopBean;
import cn.dnui_xqh414.dnuifood_16110100414.bean.CollectBean;
import cn.dnui_xqh414.dnuifood_16110100414.bean.GetAllCollectBean;
import cn.dnui_xqh414.dnuifood_16110100414.listener.OnRetrofitListener;
import cn.dnui_xqh414.dnuifood_16110100414.model.ShopModel;

public class rv_Adapter_shopCollect
        extends RecyclerView.
        Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<GetAllCollectBean> list;
    private LayoutInflater inflater;

    private int shopID,userID,foodID;



    public rv_Adapter_shopCollect(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<GetAllCollectBean> list){
        this.list = list;

        notifyDataSetChanged();   //十分重要
    }

    class shopViewHolder extends
            RecyclerView.ViewHolder {

        public View itemView;

        public TextView title,address;
        public ImageView imageView;
        ImageButton imageButton_unc;

        public shopViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_collectshopname);
            address = itemView.findViewById(R.id.tv_collectshopadd);
            imageView = itemView.findViewById(R.id.img_collectshop);
            imageButton_unc = itemView.findViewById(R.id.imgbt_uncollectshop);

            this.itemView = itemView;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder
    onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.rv_item_collectshop,
                        parent ,false);
        return new shopViewHolder(v);    //形同于 shopVIewHolder hoder = new ~;
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final GetAllCollectBean collectBean = list.get(position);
        final shopViewHolder viewHolder = (shopViewHolder) holder;
        if (collectBean == null) return;

        if (collectBean.getFlag() == 0){
            viewHolder.title.setText(collectBean.getShopname());
            viewHolder.address.setText(collectBean.getAddress());

            String url =
                    "http://172.24.10.175:8080/foodService/"+collectBean.getPic();
            Picasso.get()
                    .load(url)
                    .placeholder(R.drawable.ic_loading)
                    .error(R.drawable.ic_load_fail_sfont)
                    .fit()
                    .centerCrop()
                    .into(viewHolder.imageView);

            viewHolder.imageButton_unc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    uncollectShop(collectBean.getShop_id(),collectBean.getUser_id());
                }
            });

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    Context context = v.getContext();
                    intent.setClass(context, ShopFoodListActivity.class);
                    intent.putExtra("shopid", collectBean.getShop_id());
                    intent.putExtra("shopname", collectBean.getShopname());
                    intent.putExtra("shoppic", collectBean.getPic());
                    // TODO: 11/7/2018 启动到新的界面
                    context.startActivity(intent);
                }
            });
        }else {
            viewHolder.title.setText(collectBean.getFoodname());
            viewHolder.address.setText("￥"+collectBean.getPrice());

            String url =
                    "http://172.24.10.175:8080/foodService/"+collectBean.getPic();
            Picasso.get()
                    .load(url)
                    .placeholder(R.drawable.ic_loading)
                    .error(R.drawable.ic_load_fail_sfont)
                    .fit()
                    .centerCrop()
                    .into(viewHolder.imageView);

            viewHolder.imageButton_unc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    uncollectFood(collectBean.getFood_id(),collectBean.getUser_id());
                }
            });

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    Context context = v.getContext();
                    intent.putExtra("foodid", collectBean.getFood_id());
                    intent.setClass(context, FoodDetailActivity.class);
                    context.startActivity(intent);
                }
            });
        }

    }


    @Override
    public int getItemCount() {
        if (list == null)
            return 0;
        return list.size();
    }

    private void uncollectShop(final int shop_id, int user_id){
        this.shopID = shop_id;
        this.userID = user_id;
        ShopModel shopModel = new ShopModel();
        shopModel.doCollect(userID, shopID, new OnRetrofitListener<CollectBean>() {
            @Override
            public void onSuccess(CollectBean object, int flag) {
                if (object.getSuccess().equals("1")){
                    for (int i = 0;i<list.size();i++){
                        GetAllCollectBean bean = list.get(i);
                        if (bean.getShop_id() == shop_id){
                            list.remove(bean);
                            notifyDataSetChanged();
                            break;
                        }
                    }
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void uncollectFood(final int food_id, int user_id){
        this.foodID = food_id;
        this.userID = user_id;
        ShopModel shopModel = new ShopModel();
        shopModel.doFoodListCollect(userID, foodID, new OnRetrofitListener<CollectBean>() {
            @Override
            public void onSuccess(CollectBean object, int flag) {
                if (object.getSuccess().equals("1")){
                    for (int i = 0;i<list.size();i++){
                        GetAllCollectBean bean = list.get(i);
                        if (bean.getFood_id() == food_id){
                            list.remove(bean);
                            notifyDataSetChanged();
                            break;
                        }
                    }
                }

            }

            @Override
            public void onFailure(String msg) {

            }
        });

    }
}
